﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLite.NET;
using System.Windows.Forms;
using System.Diagnostics;

namespace BierdopjeTV.Import
{
     class Episodes
    {
        #region Fields

        //Lookup
        private int m_ShowID;
        //parse
        private string m_ShowName;
        private int m_Season; 
        private int m_Episode;        
        private string m_Codec;
        private string m_Source;
        private string m_Group;
        private string m_Path;        
        
        #endregion


        #region Properties
        
        public int ShowID
        {
            get { return m_ShowID; }
            set { m_ShowID = value; }
        }

        public string ShowName
        {
            get { return m_ShowName; }
            set { m_ShowName = value; }
        }

        public int Season
        {
            get { return m_Season; }
            set { m_Season = value; }
        }

        public int Episode
        {
            get { return m_Episode; }
            set { m_Episode = value; }
        }

 
        public string Codec
        {
            get { return m_Codec; }
            set { m_Codec = value; }
        }
        public string Source
        {
            get { return m_Source; }
            set { m_Source = value; }
        }
        public string Group
        {
            get { return m_Group; }
            set { m_Group = value; }
        }

        public string Path
        {
            get { return m_Path; }
            set { m_Path = value; }
        }

        #endregion

        //public string DATABASE = BierdopjeTV.Properties.Settings.Default["DATABASE"].ToString();


        #region methods       

        public static void addEpisode(Episodes _Episode)
        {
            string _Query;

            SQLiteResultSet _Results;
            _Query = "";
            _Query += "SELECT" + Environment.NewLine;
            _Query += "	Path" + Environment.NewLine;
            _Query += "FROM" + Environment.NewLine;
            _Query += "	Episodes" + Environment.NewLine;
            _Query += "WHERE  " + Environment.NewLine;
            _Query += "	ShowID = " + _Episode.ShowID + Environment.NewLine;
            _Query += "	AND " + Environment.NewLine;
            _Query += "	Season = " + _Episode.Season + Environment.NewLine;
            _Query += "	AND " + Environment.NewLine;
            _Query += "	Episode = " + _Episode.Episode + Environment.NewLine;
            _Query += "	AND " + Environment.NewLine;
            _Query += "	Codec = '" + _Episode.Codec + "'" + Environment.NewLine;
            _Query += "	AND " + Environment.NewLine;
            _Query += "	Source = '" + _Episode.Source + "'" + Environment.NewLine;

           
            //Check if current episode is already in the database!
            _Results = Helpers.SqliteHelper.ExecuteQuery(_Query);

            int rijen = _Results.Rows.Count;
       
            //If the current episode already exists
            if (rijen > 0)
            {
                //If the filepath is not the same as dataase
                if (_Results.Rows[0].fields[0] != _Episode.Path)
                {                    
                    //path is wrong, update!
                    Helpers.SqliteHelper.ExecuteQuery(@"UPDATE Episodes" +
                                " SET " +
                                " Path = \'" + _Episode.Path + "\'" +
                                " WHERE " +
                                " ShowID = " + _Episode.ShowID +
                                " AND" +
                                " Season = " + _Episode.Season +
                                " AND" +
                                " Episode = " + _Episode.Episode +
                                " AND" +
                                " Codec = '" + _Episode.Codec + "'" +
                                " AND" +
                                " Source = '" + _Episode.Codec + "'"
                               );
                
                     Debug.Print("Episde updated!!! (" + _Episode.Path + ")");
                }
            }
            else //the episode was not yet in the database.
            {
                Helpers.SqliteHelper.ExecuteQuery(@"INSERT INTO Episodes" +
                            "(" +
                            "    Path, ShowId, Season, Episode, Codec, Source" +
                            ")" +
                            "VALUES" +
                            "(" +
                            "    \'" + _Episode.Path + "\'," +
                            "    " + _Episode.ShowID + "," +
                            "    " + _Episode.Season + "," +
                            "    " + _Episode.Episode + "," +
                            "    \'" + _Episode.Codec + "\'," +
                            "    \'" + _Episode.Source + "\'" +
                            ")"
                           );
              
                Debug.Print("Episde added!!! (" + _Episode.Path + ")");
      
            }
     

        }


        private static string dbSave(string source)
        {
            return source.Replace("'", "''");
        }

        #endregion
    }
}
